<?php
class ImportsController extends StoreAppController {
  	var $name = 'StoreImport';
	var $uses = array('Store.StoreProduct','Store.StoreProductType','Store.StoreImport','Store.StoreUpload','Store.StoreProductUpload');
	var $components = array('Auth','Session','Cookie');
	var $permissions = array(
		'index' => array('admin','manag')
	);	
	function index(){
		$products = $this->to_array();		
		foreach ($products as $product_id => $i) {
			$this->transaction->begin($this);
			
//			$storeProduct = array();
//			$storeProduct['StoreProduct'] = $i['StoreProduct'];
//			$this->StoreProduct->id = $storeProduct['StoreProduct']['id'];
//			$this->StoreProduct->save($storeProduct);
			
//			foreach ($i['StoreProductType'] as $key => $t) {
//				$storeProductType = array();
//				$storeProductType['StoreProductType'] = $t;
//				$this->StoreProductType->create();
//				$this->StoreProductType->save($storeProductType);
//			}
			
//			foreach ($i['StoreUpload'] as $key => $u) {
//				$storeUpload = array();
//				$storeUpload['StoreUpload'] = $u;
//				$this->StoreUpload->id = $storeUpload['StoreUpload']['id'];
//				$this->StoreUpload->save($storeUpload);
//			}
//
//			foreach ($i['StoreProductUpload'] as $key => $pu) {
//				$storeProductUpload = array();
//				$storeProductUpload['StoreProductUpload'] = $pu;
//				debug($storeProductUpload);
//				$this->StoreProductUpload->create();
//				$this->StoreProductUpload->save($storeProductUpload);
//			}
			
			$this->transaction->commit($this);
		}
		debug("Konic");		
	}
	
	private function to_array(){
		$import = $this->StoreImport->find('all',array('fields'=>array('id','name','rozmiar','dlugosc_wkladki','kolor',
			'obcas','cena_detaliczna','stan','file_name','type','size','content','model_id')));
		$imports = array();
		$upload_id = 1;
		foreach ($import as $key => $i) {
			
			$imports[$i['StoreImport']['id']]['StoreProductType'][$i['StoreImport']['rozmiar']] = array(
					'product_id'=>$i['StoreImport']['id'],
					'type_id'=> $this->_type($i['StoreImport']['rozmiar']),
					'inventory'=>$i['StoreImport']['stan']
				);
			$imports[$i['StoreImport']['id']]['StoreUpload'][$i['StoreImport']['file_name']] =	array(
					'id'=>$upload_id,
					'name'=>$i['StoreImport']['file_name'],
					'type'=>$i['StoreImport']['type'],
					'size'=>$i['StoreImport']['size'],
				);

			$imports[$i['StoreImport']['id']]['StoreProductUpload'][$i['StoreImport']['file_name']] =	array(
					'upload_id'=>$upload_id,
					'product_id'=>$i['StoreImport']['id']
				);
								
			$imports[$i['StoreImport']['id']]['StoreProduct'] = array(
					'id'=>$i['StoreImport']['id'],
					'name'=>$i['StoreImport']['name'],
					'upload_id'=>$upload_id,
					'model_id' => $i['StoreImport']['model_id'],
					'currency_id'=>'PLN',
					'added'=>date('Y-m-d H:i:s'),
					'stan'=> $this->_stan($imports[$i['StoreImport']['id']]),
					'content'=> $i['StoreImport']['content'],
					'active'=>1
				);
			$upload_id++;
		}
		return $imports;
	}
	
	private function _stan($imp){
		$stan = 0;
		foreach ($imp['StoreProductType'] as $rozmiar => $s) {
			$stan += $s['inventory'];
		}
		return $stan;
	}
	
	private function _type($rozmiar){
		$type = null;
		switch($rozmiar){
			case 35:
				$type = 1;
				break;
			case 36:
				$type = 2;
				break;
			case 37:
				$type = 3;
				break;
			case 38:
				$type = 4;
				break;
			case 39:
				$type = 5;
				break;
			case 40:
				$type = 6;
				break;	
			case 41:
				$type = 7;
				break;
		}	
		return $type;	
	}
}